

import pandas as pd
import sys

#%% SET PATH ----------------------------------------------------------------------------
# When running this file usually also add argument which is the path for the table
try:
    overleaf_path = sys.argv[1]
except:
    overleaf_path = './../Apps/Overleaf/bbm/draft/reports/revision/'

#%% See how predictive is depth on well duration.
'''
wells = pd.read_csv('./data_py/temp/05_merge_with_map/wells_with_map.csv')
wells['depth_date'] = pd.to_datetime(wells['depth_date'])
wells['spud_date'] = pd.to_datetime(wells['spud_date'])
wells['drill_days'] = (wells['depth_date'] - wells['spud_date']).dt.days
reg = smf.ols(
    formula='np.log(1 + drill_days) ~ mri + I(mri**2) + I(mri**3) + I(mri**4) + I(mri**5)',
    data=wells
).fit(cov_type='HC0')
'''

#%% Why do you assume that the new contract is the same length as the original contract?
contracts = pd.read_csv('./data_py/processed/contracts_final.csv', index_col=[0])
contracts['duration_prev'] = contracts['duration'].shift(1)
contracts['tau_prev'] = contracts['tau'].shift(1)
contracts['year'] = pd.to_datetime(contracts['fixture_date']).dt.year
contracts['diff_tau'] = contracts['tau'] - contracts['tau_prev']
contracts['diff_duration'] = contracts['duration'] - contracts['duration_prev']

#%%
n_no_tau_change = ((contracts['diff_tau'] == 0.0) & (contracts['reneg'] == 1)).sum()
pct_no_tau_change_from_reneg = n_no_tau_change / (contracts['reneg'] == 1).sum()
pct_tau_change_all = ((contracts['reneg'] == 1).sum() - n_no_tau_change) / len(contracts)

#%% WRITE OUTPUT ------------------------------------------------------------------------
with open(overleaf_path + f'parameters/parameter_pct_no_tau_change_from_reneg.tex', 'w') as fout:
    fout.write(f"{round(pct_no_tau_change_from_reneg * 100, 1)}\%")

with open(overleaf_path + f'parameters/parameter_pct_tau_change_all.tex', 'w') as fout:
    fout.write(f"{round(pct_tau_change_all * 100, 1)}\%")

with open(overleaf_path + f'parameters/parameter_pct_no_tau_change_all.tex', 'w') as fout:
    fout.write(f"{round( (1 - pct_tau_change_all) * 100, 1)}\%")
